package offertest.test04;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @ClassName PCmer
 * @Description 生产者消费者模型
 * @Author ZJX
 * @Date 2025/4/12 16:29
 * @Version 1.0
 **/
public class PCmer {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue = new ArrayBlockingQueue(10);

//        生产者
        Thread t2 = new Thread(()->{
            int count = 0;
            while(true){
                try {
                    queue.put(count);
                    System.out.println("生产: "+count );
                    count++;
                    Thread.sleep(800);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

//        消费者
        Thread t1 = new Thread(()->{
            while (true){
                try {
                    System.out.println("消费: "+queue.take());
                    Thread.sleep(1200);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        t1.start();t2.start();



    }
}
